package com.jason.um.user.servlce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jason.common.error.BusinessErrorEnum;
import com.jason.common.error.BusinessException;
import com.jason.um.user.dao.UserMapper;
import com.jason.um.user.dao.UserSignMapper;
import com.jason.um.user.entity.User;
import com.jason.um.user.entity.UserSign;
import com.jason.um.user.servlce.UserService;
import com.jason.um.user.model.UserModel;
import com.jason.um.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserSignMapper userSignMapper;

    @Override
    public UserModel getUserById(Integer id) {
        User user = userMapper.selectById(id);
        // UserSign userSign = selectByUserId(id);
        return UserModel.convertFromUserEntity(user, null);
    }

    /**
     * 添加事务是否应该抛出运行时异常
     * @param userModel
     * @return
     * @throws BusinessException
     */
    @Override
    @Transactional
    public int insertUser(UserModel userModel) throws BusinessException {
        if (userModel == null) {
            return 0;
        }
        //加密用户密码
        try {
            userModel.setPassword(MD5.encryptByMD5(userModel.getPassword()));
        } catch (Exception e) {
            throw  new BusinessException(BusinessErrorEnum.UNKNOWN_ERROR);
        }
        User user = userModel.convertToUser();
        int count = userMapper.insert(user);
        if (count == 0){
            throw new RuntimeException();
        }
        UserSign userSign = userModel.convertToUserSign();

        userSign.setUserId(user.getId());

        count = userSignMapper.insert(userSign);
        if (count == 0 ){
            throw new RuntimeException();
        }
        return count;
    }

    @Override
    public Boolean authenticate(String telephone, String password) throws BusinessException {

        Boolean success = false;
        User user = selectUserByTelephone(telephone);
        if (ObjectUtils.isEmpty(user)) {
            throw new BusinessException(BusinessErrorEnum.USER_NOT_EXIST);
        }
        UserSign userSign = selectByUserId(user.getId());
        try {
            success = MD5.authenticate(password, userSign.getPassword());
        } catch (Exception e) {
            throw new BusinessException(BusinessErrorEnum.USER_AUTH_FAILED);
        }
        return success;
    }

    @Override
    public UserModel getUserByTelephone(String telephone) {
        User user = selectUserByTelephone(telephone);
        return UserModel.convertFromUserEntity(user, null);
    }

    private User selectUserByTelephone(String telephone){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getTelephone, telephone);
        return userMapper.selectOne(wrapper);
    }

    private UserSign selectByUserId(Integer userId){
        LambdaQueryWrapper<UserSign> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserSign::getUserId, userId);
        return userSignMapper.selectOne(wrapper);
    }
}
